using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDF.Data;
using DDF.Data.Providers;
using System.Data;
using System.Linq.Expressions;
using System.Configuration;
using Chinesys.DataModel.Gateways;

namespace Chinesys.DataModel.Repositories
{
	public static partial class ProductsRepository
	{
		private static string connString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
		
		public class ProductsDbo : DatabaseObject
        {
            public ProductsDbo()
            {
                Name = "Products";
                GeneratedClassName = "Products";
                SequenceName = "N/A";
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Id", ColumnType = "int", IsIdentity = true, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "Id" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "CategoryId", ColumnType = "int", IsIdentity = false, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "CategoryId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "SubCategoryId", ColumnType = "int", IsIdentity = false, IsNullable = false, Length = 4, Precision = 10, Scale = 0, GeneratedPropertyName = "SubCategoryId" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Description", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "Description" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Price", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 20, Precision = 0, Scale = 0, GeneratedPropertyName = "Price" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "ImageSrc", ColumnType = "varchar", IsIdentity = false, IsNullable = false, Length = 200, Precision = 0, Scale = 0, GeneratedPropertyName = "ImageSrc" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "Rating", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "Rating" });
					Columns.Add(new DatabaseObjectColumn() { ColumnName = "TotalReviews", ColumnType = "varchar", IsIdentity = false, IsNullable = true, Length = 50, Precision = 0, Scale = 0, GeneratedPropertyName = "TotalReviews" });
              
            }
        }
		
		public static List<ProductRow> Select(Expression<Func<ProductRow, bool>> expression)
        {
            List<ProductRow> rows = new List<ProductRow>();

            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                IDataReader reader = database.Select<ProductRow>(new ProductsDbo(), expression);

                Load(reader, rows);
            }

            return rows;
        }
		
		public static List<ProductRow> Select()
		{
			List<ProductRow> rows = new List<ProductRow>();

			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.Select<ProductRow>(new ProductsDbo(), null);

				Load(reader, rows);
			}

			return rows;
		}
		
		public static List<ProductRow> PagedSelect(Expression<Func<ProductRow, bool>> filterExpression, Expression<Func<ProductRow, object>> orderByExpression, int pageNumber, int pageSize)
		{
			List<ProductRow> rows = new List<ProductRow>();
			
			using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
			{
				IDataReader reader = database.PagedSelect<ProductRow>(new ProductsDbo(), filterExpression, orderByExpression, pageNumber, pageSize);

				Load(reader, rows);
			}

			return rows;
		}
		
		private static void Load(IDataReader reader, List<ProductRow> ProductList)
        {
            ProductRow row;

            while (reader.Read())
            {
                row = new ProductRow()
                {
					Id = Util.CastDbValue<int>(reader["Id"]),
					CategoryId = Util.CastDbValue<int>(reader["CategoryId"]),
					SubCategoryId = Util.CastDbValue<int>(reader["SubCategoryId"]),
					Description = Util.CastDbValue<string>(reader["Description"]),
					Price = Util.CastDbValue<string>(reader["Price"]),
					ImageSrc = Util.CastDbValue<string>(reader["ImageSrc"]),
					Rating = Util.CastDbValue<string>(reader["Rating"]),
					TotalReviews = Util.CastDbValue<string>(reader["TotalReviews"])
                };
                                
                ProductList.Add(row);
            }

            reader.Close();
        }

			
		public static void Insert(ProductRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Insert<ProductRow>(new ProductsDbo(), row);
            }
        }

		public static void Update(ProductRow row)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Update<ProductRow>(new ProductsDbo(), row);
            }
        }

		public static void Delete(Expression<Func<ProductRow, bool>> expression)
        {
            using (Database database = new SqlDataProvider(connString, "{0}Cursor", "dbo"))
            {
                database.Delete<ProductRow>(new ProductsDbo(), expression);
            }
        }

	}
}
	
